const cloud = require('wx-server-sdk')
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })

exports.main = async (event, context) => {
  try {
    const db = cloud.database()
    const { id } = event
    
    if (!id) {
      return { code: 400, message: '缺少必要参数id' }
    }

    const res = await db.collection('Pois').doc(id).get()
    
    if (!res.data) {
      return { code: 404, message: '未找到对应数据' }
    }


  // 添加数据格式化层
  const safeData = res.data || {}
  const formattedData = {
    address: safeData.address || '地址未提供',
    adname: safeData.adname || '',
    location: safeData.location ? 
      [safeData.location.longitude, safeData.location.latitude] : 
      [0, 0],
    name: safeData.name || '未知名称',
    opentime2: Array.isArray(safeData.opentime2) ? safeData.opentime2 : [],
    photos: Array.isArray(safeData.photos) ? safeData.photos : 'cloud://dp-6gydzcp27ac11774.6470-dp-6gydzcp27ac11774-1325695033/默认图片/NB-Map.png',
    tel: Array.isArray(safeData.tel) ? safeData.tel : [],
    type: safeData.type || '未分类' ,// 确保type字段始终存在
    islike:safeData.islike || false
  }

  return {
    code: 200,
    data: formattedData,
    message: '获取成功'
  }
} catch (err) {
  console.error('云函数错误：', err)
  return { 
    code: 500, 
    data: null,  // 明确返回null
    message: '服务器错误' 
  }
}
}